<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01_非受控组件</title>
</head>

<body>
    <div id="app"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class MyForm extends React.Component{
            userName = React.createRef()
            passWord = React.createRef()

            submitForm = (event)=>{
                console.log(event);
                // 阻止表单提交默认事件
                event.preventDefault()
                // 从this中解构获取ref
                const {userName,passWord} = this
                // 点击提交数据时从ref中获取表单内容
                alert(`用户名是：${userName.current.value},密码是：${passWord.current.value}`)
            }
            render(){
                return (
                    <form onSubmit={this.submitForm}>
                        用户名：<input type="text" name="username" ref={this.userName}/><br/>
                        密码：<input type="password" name="password" ref={this.passWord}/><br/>
                        <button>登录</button>
                    </form>
                )
            }
        }

        ReactDOM.render(<MyForm/>,document.getElementById("app"))
    </script>
</body>

</html>